package com.qiankun.tang.ui.view.adapter.find;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.net.items.BaseItem;
import com.common.library.ui.view.adapter.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.HotItem;
import com.qiankun.tang.ui.view.AutoPlayADWidget;
import com.qiankun.tang.ui.view.adapter.holder.BaseLineHolder;
import com.qiankun.tang.ui.view.adapter.holder.BaseTitleHolder;

import java.util.List;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter
 * @date 2017/6/9 0009
 */

public class HotAdapter extends BaseRecyclerAdapter<HotItem> {

    public HotAdapter(Context context) {
        super(context);
    }

    public HotAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }
    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HotItem.TYPE_0:
                return new HotViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_hot_0, parent, false));
            case HotItem.TYPE_1:
                return new HotViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_hot_1, parent, false));
            case BaseItem.TYPE_TITLE:
                return new BaseTitleHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_title, parent, false));
            case BaseItem.TYPE_LINE:
                return new BaseLineHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_line, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HotViewHolder0) {
            onBindHotViewHolder0((HotViewHolder0) holder, position);
        }else if(holder instanceof HotViewHolder1){
            onBindHotViewHolder1((HotViewHolder1) holder, position);
        }else if(holder instanceof BaseTitleHolder){
            onBindBaseTitleHolder((BaseTitleHolder) holder, position);
        }
    }
    private void onBindBaseTitleHolder(BaseTitleHolder holder, int position) {
        String title = (String) getDatas().get(position).getBean();
        holder.mTitle.setText(title);
    }
    private void onBindHotViewHolder1(HotViewHolder1 holder, int position) {
       final VideoSynopsisBean bean = (VideoSynopsisBean) getDatas().get(position).getBean();
        holder.mImge.setImageURI(bean.getImgUrl());
        holder.mTitle.setText(bean.getTitle());
        holder.mSubTitle.setText(bean.getSubtitle());
        holder.mIcon.setImageURI(bean.getIconUrl());
        holder.mData.setText(bean.getData());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListener().onInteraction(HotItem.TYPE_1,bean);
            }
        });
    }

    private void onBindHotViewHolder0(HotViewHolder0 holder, int position) {
        List<ADBean> datas = (List<ADBean>) getDatas().get(position).getBean();
        holder.ADWidget.setData(datas,true, 0,4000);
        holder.ADWidget.addOnClickListaner(new AutoPlayADWidget.OnClickListaner() {
            @Override
            public void onItemClick(ADBean bean) {
                getListener().onInteraction(HotItem.TYPE_0,bean);
            }
        });
    }
    class HotViewHolder0 extends RecyclerView.ViewHolder {
        AutoPlayADWidget ADWidget;
        public HotViewHolder0(View itemView) {
            super(itemView);
            ADWidget = (AutoPlayADWidget) itemView.findViewById(R.id.item_home_AutoPlayADWidgett);
        }
    }
    class HotViewHolder1 extends RecyclerView.ViewHolder {
        SimpleDraweeView mImge,mIcon;
        TextView mTitle,mSubTitle,mData;
        public HotViewHolder1(View itemView) {
            super(itemView);
            mImge = (SimpleDraweeView) itemView.findViewById(R.id.item_hot_1_img);
            mIcon = (SimpleDraweeView) itemView.findViewById(R.id.item_hot_1_icon);

            mData= (TextView) itemView.findViewById(R.id.item_hot_1_data);
            mTitle= (TextView) itemView.findViewById(R.id.item_hot_1_title);
            mSubTitle = (TextView) itemView.findViewById(R.id.item_hot_1_subtitle);
        }
    }
}
